<?php
/*
usage:
Hbuilder 打开 addons/xpframework/uniapp/xpframework_mobile , 菜单：发行-网站PC WEB或手机H5
php xp_project_console_package_plugin.php xpframework env_plugin
php xp_project_console_package_plugin.php xpframework env_dev

* 检查打包的代码： https://www.fastadmin.net/developer/addoncheck.html
*/
// 获取传入的参数
$pluginname = $argv[1]; // 默认为 'xpframework'
$env = $argv[2];

// include "xp_function.php";
include "xp_project_function.php";
$src_plugin = "addons/{$pluginname}";
$dist_plugin = "dist/{$pluginname}";
$deploy_plugin = "addons/{$pluginname}/_deploy/{$env}";


deleteDirectory($dist_plugin);
createDirectory($dist_plugin);

xcopy($src_plugin, $dist_plugin);

$modules = ["admin", "api", "index", $pluginname];
foreach ($modules as $module){
    createDirectory("{$dist_plugin}/application");
    createDirectory("{$dist_plugin}/application/{$module}");

    createDirectory("{$dist_plugin}/application/{$module}/controller");
    createDirectory("{$dist_plugin}/application/{$module}/controller/{$pluginname}");
    xcopy("application/{$module}/controller/{$pluginname}", "{$dist_plugin}/application/{$module}/controller/{$pluginname}");

    createDirectory("{$dist_plugin}/application/{$module}/lang");
    createDirectory("{$dist_plugin}/application/{$module}/lang/zh-cn");
    createDirectory("{$dist_plugin}/application/{$module}/lang/zh-cn/{$pluginname}");
    xcopy("application/{$module}/lang/zh-cn/{$pluginname}", "{$dist_plugin}/application/{$module}/lang/zh-cn/{$pluginname}");

    createDirectory("{$dist_plugin}/application/{$module}/model");
    createDirectory("{$dist_plugin}/application/{$module}/model/{$pluginname}");
    xcopy("application/{$module}/model/{$pluginname}", "{$dist_plugin}/application/{$module}/model/{$pluginname}");

    createDirectory("{$dist_plugin}/application/{$module}/validate");
    createDirectory("{$dist_plugin}/application/{$module}/validate/{$pluginname}");
    xcopy("application/{$module}/validate/{$pluginname}", "{$dist_plugin}/application/{$module}/validate/{$pluginname}");

    createDirectory("{$dist_plugin}/application/{$module}/view");
    createDirectory("{$dist_plugin}/application/{$module}/view/{$pluginname}");
    xcopy("application/{$module}/view/{$pluginname}", "{$dist_plugin}/application/{$module}/view/{$pluginname}");
}

createDirectory("{$dist_plugin}/assets");
xcopy("public/assets/addons/{$pluginname}","{$dist_plugin}/assets");

createDirectory("{$dist_plugin}/public");
createDirectory("{$dist_plugin}/public/assets");
createDirectory("{$dist_plugin}/public/assets/js");
createDirectory("{$dist_plugin}/public/assets/js/backend");
createDirectory("{$dist_plugin}/public/assets/js/frontend");
createDirectory("{$dist_plugin}/public/assets/js/backend/{$pluginname}");
createDirectory("{$dist_plugin}/public/assets/js/frontend/{$pluginname}");
xcopy("public/assets/js/backend/{$pluginname}","{$dist_plugin}/public/assets/js/backend/{$pluginname}");
xcopy("public/assets/js/frontend/{$pluginname}","{$dist_plugin}/public/assets/js/frontend/{$pluginname}");


// deploy env's config
xcopy($deploy_plugin,$dist_plugin);

// delete 
deleteDirectory("{$dist_plugin}/_deploy");
deleteDirectory("{$dist_plugin}/uniapp/{$pluginname}_mobile/node_modules");
deleteDirectory("{$dist_plugin}/uniapp/{$pluginname}_mobile/unpackage");
deleteFile($dist_plugin, "/Dmo.*/");
deleteFile($dist_plugin, "/dmo.*/");
deleteFile($dist_plugin,"/.DS_Store/");

// package
$ini = parse_ini_file("{$src_plugin}/info.ini");
$version = $ini["version"];
zipDirectory($dist_plugin,"dist/{$pluginname}-{$version}.zip");